package com.xiaoqiang.git.hellojavaserialportnew.model;

import java.io.Serializable;

public class Reply<T> implements Serializable {
    /**
     * 应答码
     * OK：200
     * ERROR:400
     */
    private String code;
    /**
     * 业务数据
     */
    private T data;
    /**
     * 时间
     */
    private Long time = System.currentTimeMillis();
    /**
     * 消息
     */
    private String msg;
    /**
     * 附加信息
     */
    private Object annex;

    public <T> Reply() {

    }

    public Reply(ResultCode resultCode, T defaultMessage) {
        this.setCode(resultCode.getCode());
        this.setMsg(resultCode.getMsg());
        this.setData(defaultMessage);
    }

    public Reply(String code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }
    public static <T> Reply<T> success() {
        return restResult(null, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> Reply<T> success(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> Reply<T> fail(T data, String msg) {
        return restResult(data, ResultCode.FAILED.getCode(), msg);
    }

    public static <T> Reply<T> fail(int code, String msg) {
        return restResult(null, String.valueOf(code), msg);
    }

    public static <T> Reply<T> fail(String code, String msg) {

        return restResult(null, code, msg);
    }

    private static <T> Reply<T> restResult(T data, String code, String msg) {
        Reply<T> apiResult = new Reply<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public String getCode() {
        return code;
    }

    public Reply setCode(String code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return data;
    }

    public Reply setData(T data) {
        this.data = data;
        return this;
    }

    public Long getTime() {
        return time;
    }

    public Reply setTime(Long time) {
        this.time = time;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Reply setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getAnnex() {
        return annex;
    }

    public Reply setAnnex(Object annex) {
        this.annex = annex;
        return this;
    }

    public boolean isSuccess() {
        return "200".equals(getCode());
    }

}
